package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.ProjectDTO;
import com.neusoft.neuiotms.dcom.model.Project;
import com.neusoft.neuiotms.dcom.repository.ProjectRepository;

@Component
public class ProjectConverter extends AbstractConverter<Project, ProjectDTO> {

    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private BaseIdGenerator projectCodeGenerator;

    @Override
    public Project toModel(final ProjectDTO dto) {

        Project model;
        if (dto.isNew()) {
            model = new Project();
            model.setCode(this.projectCodeGenerator.generateCode());
        } else {
            model = this.projectRepository.getOne(dto.getId());
            model.setCode(dto.getCode());
        }

        model.setName(dto.getName());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setSlightPromptInteval(dto.getSlightPromptInteval());
        model.setGeneralPromptInteval(dto.getGeneralPromptInteval());
        model.setSeriousPromptInteval(dto.getSeriousPromptInteval());

        return model;
    }

    @Override
    public ProjectDTO toDTO(final Project model, final boolean forListView) {

        final ProjectDTO dto = new ProjectDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setSlightPromptInteval(model.getSlightPromptInteval());
        dto.setGeneralPromptInteval(model.getGeneralPromptInteval());
        dto.setSeriousPromptInteval(model.getSeriousPromptInteval());

        return dto;
    }

}
